<?php
require_once 'Lib/main.php';
require_once 'Lib/common/commonConst.php';
require_once 'Lib/Config/config.php';

class RestrictAction {
	public function __construct() {
	}
	
	/**
	 * Check Restrict
	 * @param $ipAddr
	 * @return String $content
	 */
	public function Restriction($ipAddr) {	
		try {			
			$restrictDao  = new Dao_RestrictDAO();
			//$delRes       = $restrictDao->delRestrictByExpireDate();
	
			$restrictInfo = $restrictDao->getRestrictByIp($ipAddr);
			
			if ($restrictInfo !== null && count($restrictInfo) > 0) {			
				//$restrictInfo = count($restrictInfo) > 1 ? $restrictInfo[0] : $restrictInfo;
				$restrictInfo = $restrictInfo[0];
				
				$restrictId           = $restrictInfo->getId();
				$restrictCurrentCount = $restrictInfo->getCurrentCount();
				$restrictMaxCount     = $restrictInfo->getMaxCount();
				
				// Check restrict for send mail
				$isRetrict = $restrictDao->getRestrictByEndRestrictDate($restrictId);
				
				if(count($isRetrict) > 0){
					$isRetrict = true;				
				} else {
					$isRetrict = false;
					// Update
					$restrictCurrentCount++;
					if ($restrictCurrentCount > $restrictMaxCount) {	
						$restrictInfo->setStartRestrictDate('NOW()');
						$restrictInfo->setEndRestrictDate('DATE_ADD(NOW(),INTERVAL 1 DAY)');
					} else {
						$restrictInfo->setCurrentCount($restrictCurrentCount);
						$restrictInfo->setStartRestrictDate(null);
						$restrictInfo->setEndRestrictDate(null);
					}

					$updateRes = $restrictDao->updateRestrict($restrictInfo);
					
					//echo $updateRes;die();					
					
				}
				
				return $isRetrict;		
			} else {
				// Insert
				$restrictMaxCount = 10;
				$ipRestrict       = $this->getClientIp();
				$typeRestrict     = 1;
				$reasonRestrict   = 'send_mail_limit';
				
				$restrictInfo = new Model_Restrict(null);
				$restrictInfo->setMaxCount($restrictMaxCount);
				$restrictInfo->setIpRestrict($ipRestrict);
				$restrictInfo->setTypeRestrict($typeRestrict);
				$restrictInfo->setReasonRestrict($reasonRestrict);
				
				$insertRes = $restrictDao->insertRestrict($restrictInfo);
				
				return false;
			}
			
		} catch (Exception $e) {
			echo json_encode(array("success" => false,"error" => $e->getMessage()));
			exit;
		}
	}
	
	public function getClientIp() {
	     $ipaddress = '';
	     if (getenv('HTTP_CLIENT_IP')) {
	         $ipaddress = getenv('HTTP_CLIENT_IP');
	     } else if (getenv('HTTP_X_FORWARDED_FOR')) {
	         $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
	     } else if (getenv('HTTP_X_FORWARDED')) {
	         $ipaddress = getenv('HTTP_X_FORWARDED');
	     } else if (getenv('HTTP_FORWARDED_FOR')) {
	         $ipaddress = getenv('HTTP_FORWARDED_FOR');
	     } else if (getenv('HTTP_FORWARDED')) {
	         $ipaddress = getenv('HTTP_FORWARDED');
	     } else if (getenv('REMOTE_ADDR')) {
	         $ipaddress = getenv('REMOTE_ADDR');
	     } else {
	         $ipaddress = 'UNKNOWN';
	     }
	     
	     return $ipaddress; 
	}

}

$restrict = new RestrictAction(); 
$clientIpAddr = $restrict->getClientIp();
$isRetrict = $restrict->Restriction($clientIpAddr);

if (!$isRetrict) {
	require_once 'Lib/SendMailAction.php';
	$sending = new SendMailAction(); 
	echo $sending->sendContactMail($_REQUEST);
} else {
	echo json_encode(array("success" => false, "error" => "Ban Bi Khoa Gui Mail"));	
}
